<?php

namespace App\Http\Controllers\RBAC;

use App\RBAC\Operation;
use App\RBAC\Resource;
use App\RBAC\Privilege;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class OperationController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $request->session()->put('status', 'current_user:'.Auth::user());
        return view('rbac/operation');
    }

    public function apiIndex(Request $request)
    {
         $paginateData=Operation::orderBy('id',"ASC")->paginate(intval($request->per_page));
         foreach ($paginateData as $key => $value) {
            $tmp=$value->resources;
         }
         $data['data']=$paginateData;
         $data['resources']=Resource::where('pid',0)->get();
         return $data;
    }

    public function apiDelete(Request $request,Operation $operation)
    {
        $resources=$operation->resources;
        foreach ($resources as $key => $value) {
            $value->pivot->delete();
        }
        $roles=$operation->roles;
        foreach ($roles as $key => $value) {
            $value->pivot->delete();
        }
        if($operation->delete()){
            return ['msg'=>'ok'];
        }else{
            return ['msg'=>'fail'];
        }
    }

    public function apiStore(Request $request)
    {
        if($request->id){
            $group = Operation::findOrFail(intval($request->id));
            $group->name=$request->name;
            $group->description=$request->description;
            if($group->save()){
                $group = Operation::findOrFail(intval($request->id));
            }
        }else{
            $group = Operation::firstOrCreate(['name' => $request->name,'description'=>$request->description]);
        }
        $resources=$request->resources;
        $oldResources=$request->oldResources;
        if($request->resources){            
            $operation_id=$group->id;
            foreach ($resources as $key => $value) {
                if(!in_array($value,$oldResources)){
                    Privilege::firstOrCreate([
                        'operation_id'=>$operation_id,
                        'resource_id'=>$value,
                    ]);
                }     
            }
        }
        if($request->oldResources){            
            $operation_id=$group->id;
            foreach ($oldResources as $key => $value) {
                if(!in_array($value,$resources)){
                    $privilege=Privilege::where('resource_id',$value)->where('operation_id',$operation_id)->first();
                    if($privilege){
                        $privilege->delete();
                    }
                }
            }
        }

        $tmp=$group->resources;

        return $group;
    }
}
